﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqProviders.LinqToNaver
{
    /// <summary>
    /// 오타변환
    /// </summary>
    public class ErrataSearchResult : NaverSearchResult
    {
        /// <summary>
        /// Gets or sets the keyword. 검색 키워드.
        /// </summary>
        /// <value>The keyword.</value>
        public string Keyword { get; set; }

        /// <summary>
        /// Gets the channel query.
        /// </summary>
        /// <param name="xdoc">The xdoc.</param>
        /// <returns></returns>
        private IEnumerable<ErrataSearchResult> Parse(string input)
        {
            if (string.IsNullOrEmpty(input))
                return null;

            XDocument xdoc = XDocument.Parse(input);
            var result = xdoc.Descendants("result").FirstOrDefault();

            return from items in result.Descendants("item")
                   select new ErrataSearchResult
                   {
                       Keyword = items.Value
                   };
        }

        protected override object Parse(string input, string requestedQuery, out int count, out int totalCount)
        {
            IEnumerable<ErrataSearchResult> resultList = new List<ErrataSearchResult>();
            count = 0;
            totalCount = 0;

            if (String.IsNullOrEmpty(input) == false)
            {
                resultList = Parse(input);
            }

            totalCount = resultList.FirstOrDefault() != null ? resultList.FirstOrDefault().Total : 0;

            return resultList;
        }
    }

    
}
